package com.team.six.bluecatshop.service;

import com.team.six.bluecatshop.domain.MiaoSha;
import com.team.six.bluecatshop.mapper.MiaoShaMapper;
import com.team.six.bluecatshop.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MiaoShaServiceImpl implements MiaoShaService {
    @Autowired
    private MiaoShaMapper miaoShaMapper;
    @Resource
    private RedisUtil redisUtil;

    @Override
    @Transactional
    public void add(MiaoSha miaoSha) {
        miaoShaMapper.insert(miaoSha);
    }

    @Override
    @Transactional
    public void remove(String id) {
        miaoShaMapper.deleteByPrimaryKey(id);
    }

    @Override
    public MiaoSha findById(String id) {
        MiaoSha miaoSha=miaoShaMapper.selectByPrimaryKey(id);
        return miaoSha;
    }

    @Override
    public List<MiaoSha> findAll() {
        List<MiaoSha> miaoShaList = miaoShaMapper.findAll();
        return miaoShaList;
    }

    @Override
    public MiaoSha findMiaoShaByProductId(String id) {
        MiaoSha miaoSha = miaoShaMapper.findByProductId(id);
        return miaoSha;
    }


    @Override
    @Transactional
    public void updateByPrimaryKey(MiaoSha miaoSha) {
        miaoShaMapper.updateByPrimaryKeySelective(miaoSha);
    }

    @Override
    /**
     * 查看剩余秒杀商品数量
     * */
    public boolean verifyCount(String miaoshaId){
        int miaoshaCount = (int) redisUtil.get("miaosha" + miaoshaId);
        if (miaoshaCount>0){
            System.out.println("miaosha"+miaoshaId+"还剩"+miaoshaCount);
            return true;
        }
        System.out.println("miaosha"+miaoshaId+"还剩"+miaoshaCount);
        return false;
    }
}
